package core.september.cloudbadge.panel.client.navbar;

import com.github.gwtbootstrap.client.ui.Nav;
import com.github.gwtbootstrap.client.ui.NavCollapse;
import com.github.gwtbootstrap.client.ui.NavLink;
import com.github.gwtbootstrap.client.ui.constants.Alignment;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;

import core.september.cloudbadge.panel.client.credentials.credentialService;
import core.september.cloudbadge.panel.client.credentials.credentialServiceAsync;

public class NavBinder extends Composite {

	private static PanelBinderUiBinder uiBinder = GWT
			.create(PanelBinderUiBinder.class);
//	@UiField(provided=true) CellList<Object> cellList = new CellList<Object>(new AbstractCell<Object>(){
//		@Override
//		public void render(Context context, Object value, SafeHtmlBuilder sb) {
//			// TODO
//		}
//	});
//	
	@UiField(provided=true) NavCollapse navcollapse = new NavCollapse();
	

	interface PanelBinderUiBinder extends UiBinder<Widget, NavBinder> {
	}
	
	private final credentialServiceAsync credentialServlet = GWT.create(credentialService.class);

	public NavBinder() {
		initWidget(uiBinder.createAndBindUi(this));
	}

	public NavBinder(String... values) {
		initWidget(uiBinder.createAndBindUi(this));
		
		Nav nav = new Nav();
		nav.setAlignment(Alignment.RIGHT);
		navcollapse.add(nav);
		for (String value: values) {
			NavLink link = new NavLink();
			link.setText(value);
			link.addClickHandler(new ClickHandler() {
				
				@Override
				public void onClick(ClickEvent event) {
					
					
				}
			});
			
			nav.add(link);
			}
		
		final NavLink link = new NavLink();
		
		credentialServlet.logInOutLabel(new AsyncCallback<String>() {
			
			@Override
			public void onSuccess(String result) {
				link.setText(result);
				
			}
			
			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}
		});
		
		credentialServlet.logInOutUrl(Window.Location.getHref(), new AsyncCallback<String>() {
			
			@Override
			public void onSuccess(String result) {
				link.setHref(result);
				
			}
			
			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}
		});
		
		
		nav.add(link);
		
		}
	}


